package com.taotao.manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manager.service.ItemCatService;
import com.taotao.pojo.TbItemCat;

@Controller
@RequestMapping("item/cat")
public class ItemCatController {

	@Autowired
	private ItemCatService itemCatService;

	/**
	 * 
	 * <p>Title: 查询商品类目列表</p>
	 * <p>Description: </p>
	 * @param parentId
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<TbItemCat>> queryItemListByParentId(
			@RequestParam(value = "id", defaultValue = "0") long parentId) {
		try {
			TbItemCat tbItemCat = new TbItemCat();
			tbItemCat.setParentId(parentId);
			List<TbItemCat> itemCats = this.itemCatService.queryListByWhere(tbItemCat);
			if(itemCats == null || itemCats.isEmpty()) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(itemCats);
		} catch (Exception e) {
			 e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 
	 * <p>Title: 查询商品类目</p>
	 * <p>Description: </p>
	 * @param cid
	 * @return
	 */
	@RequestMapping(value="/inf/{cid}",method = RequestMethod.GET)
	public ResponseEntity<TbItemCat> queryItemById(
			@PathVariable(value = "cid") long cid) {
		try {
			TbItemCat itemCat = this.itemCatService.queryById(cid);
			if(itemCat == null) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(itemCat);
		} catch (Exception e) {
			 e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
}
